//
//  Language.m
//  DaLong
//
//  Created by keen on 13-10-24.
//  Copyright (c) 2013年 keen. All rights reserved.
//

#import "Language.h"

static NSDictionary* _currentLanguageDic;

@implementation Language

+ (void)initializeLanguage {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* lanKey = [defaults objectForKey:kLanguageCurrent];
    if (lanKey == nil) {
        lanKey = @"zh-Hans";
    }
    [self setLanguage:lanKey];
}

// en:英文    zh-Hans:简体中文    zh-Hant:繁体中文    ru:俄语
+ (NSString*)getPreferredLanguage
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString* preferredLang = [languages objectAtIndex:0];
    NSLog(@"获取系统语言:%@",preferredLang);
    return preferredLang;
}

+ (void)setLanguage:(NSString*)key {
    NSString* path = [[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"];
    NSArray* arr = [NSArray arrayWithContentsOfFile:path];
    _currentLanguageDic = nil;
    if (key) {
        for (NSDictionary* dic in arr) {
            if ([[dic objectForKey:@"key"] isEqual:key]) {
                _currentLanguageDic = dic;
                break;
            }
        }
    }
    if (_currentLanguageDic == nil) {
        key = kLanguageDefault;
        for (NSDictionary* dic in arr) {
            if ([[dic objectForKey:@"key"] isEqual:key]) {
                _currentLanguageDic = dic;
                break;
            }
        }
    }
    NSLog(@"设置当前语言为 : %@ -> %@",key,[self getStringWithKey:@"name"]);
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setValue:key forKey:kLanguageCurrent];
    [defaults synchronize];
}

+ (NSString*)getStringWithKey:(NSString*)key {
    NSString* str = nil;
    
    str = [_currentLanguageDic objectForKey:key];
    
    if (str == nil) {
        str = key;
    }
    return str;
}

+ (NSArray*)getLanguageArr {
    NSString* path = [[NSBundle mainBundle] pathForResource:@"Language" ofType:@"plist"];
    return [NSArray arrayWithContentsOfFile:path];
}

@end
